//
//  BWUserDomain.m
//  BWCommonModelModule
//
//  Created by Xun on 2023/12/18.
//

#import "BWUserDomain.h"
#import <BWDatabaseModule/BWDatabaseModule-umbrella.h>

@implementation BWUserDomain
#pragma mark - 初始化
- (instancetype)init {
    self = [super init];
    if (self) {
        self.customData = [[BWCustomDataDomain alloc] init];
        self.mNeedSecurityProperties = @[propertyKeyName(self.CellPhone), propertyKeyName(self.NickName)];
    }
    return self;
}

#pragma mark - 保存一条记录
- (void)saveOrUpdate {
    [[BWDatabaseManager shared].dbQueue inDatabase:^(FMDatabase * _Nonnull db) {
        if (![db open]) {
            NSLog(@"Could not open db.");
        }
        NSString *sqlString = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE eid = ?", TABLE_NAME_USER_DOMAIN];
        BOOL bFind = NO;
        FMResultSet *rs = [db executeQuery:sqlString, self.EID];
        while ([rs next]) {
            bFind = YES;
            break;
        }
        [rs close];
        
        if (!bFind) {
            sqlString = [NSString stringWithFormat:@"INSERT INTO %@ (cell_phone, nick_name, relation, headkey, reserved1, reserved2, reserved3, eid) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", TABLE_NAME_USER_DOMAIN];
        } else {
            sqlString = [NSString stringWithFormat:@"UPDATE %@ SET cell_phone= ?, nick_name= ?, relation= ?, headkey= ?, reserved1= ?, reserved2= ?, reserved3= ? WHERE eid= ?", TABLE_NAME_USER_DOMAIN];
        }
        
        NSString *relationJsonStr = nil;
        if ([NSJSONSerialization isValidJSONObject:self.customData.relation]) {
            relationJsonStr = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:self.customData.relation options:NSJSONWritingPrettyPrinted error:nil]
                                                    encoding:NSUTF8StringEncoding];
        }
        
        [self encryptForPropertyArray];
//        NSString *encryptRelationJsonStr = [AESUtil encrypt:relationJsonStr];
//        [db executeUpdate:sqlString, self.CellPhone, self.NickName, encryptRelationJsonStr, self.customData.headkey, self.reserved1, self.reserved2, self.reserved3, self.EID];
        [self decryptForPropertyArray];
    }];
}

#pragma mark - 根据userEID查找数据库中存储的BWUserDomain
+ (BWUserDomain *)findObjectByUserEID:(NSString *)userEID {
    __block BWUserDomain *userDomain = nil;
    [[BWDatabaseManager shared].dbQueue inDatabase:^(FMDatabase * _Nonnull db) {
        if (![db open]) {
            NSLog(@"Could not open db.");
        }
        
        NSString *sqlString = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE eid = ?", TABLE_NAME_USER_DOMAIN];
        FMResultSet *rs = [db executeQuery:sqlString, userEID];
        while ([rs next]) {
            userDomain = [[BWUserDomain alloc] init];
            userDomain.CellPhone = [rs stringForColumn:@"cell_phone"];
            userDomain.NickName = [rs stringForColumn:@"nick_name"];
            userDomain.customData.headkey = [rs stringForColumn:@"headkey"];
            userDomain.reserved1 = [rs stringForColumn:@"reserved1"];
            userDomain.reserved2 = [rs stringForColumn:@"reserved2"];
            userDomain.reserved3 = [rs stringForColumn:@"reserved3"];
            userDomain.EID = [rs stringForColumn:@"eid"];
            userDomain.mId = [rs intForColumn:@"id"];
            
//            NSString *relationJsonStr = [AESUtil decrypt:[rs stringForColumn:@"relation"]];
//            if (relationJsonStr != nil) {
//                NSData *jsonData = [relationJsonStr dataUsingEncoding:NSUTF8StringEncoding];
//                id relationJsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
//                if ([relationJsonObject isKindOfClass:[NSDictionary class]]) {
//                    userDomain.customData.relation = [[NSMutableDictionary alloc] initWithDictionary:relationJsonObject copyItems:YES];
//                }
//            }
            [userDomain decryptForPropertyArray];
            break;
        }
        
        [rs close];

    }];
    return userDomain;
}
@end
